package com.czjtxy.extraTerm.controller;

import com.czjtxy.extraTerm.entity.DO.Tuser;
import com.czjtxy.extraTerm.server.UserService;
import com.czjtxy.extraTerm.serverImpl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @program: staff-house
 * @author: MingYi
 * @create: 2021/06/29 12:49
 */
@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doGet(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String loginName = request.getParameter("loginname");
        String pwd = request.getParameter("password");
        String result = "";
        UserService userService = new UserServiceImpl();
        Tuser tuser = null;
        try {
            tuser = userService.login(loginName, pwd);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        if (tuser != null) {
            result = "main.jsp";
        } else {
            result = "loginForm.jsp";
        }
        request.getRequestDispatcher(result).forward(request, response);
    }
}
